<?php

namespace app\api\controller;

use app\data\Data;
use app\api\model\News;
use think\Db;
use app\api\model\Form;

class Trading extends Index
{
    public function get_category()
    {
        $category_id = (isset($_GET['p_id']) && !empty($_GET['p_id'])) ? $_GET['p_id'] : 1;
        $category = db('market_trading_category')->where('id', $category_id)->find();
        $res = db('market_trading_category')->where('pid', $category['id'])->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '成功', $res);

        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

    public function get_c_id()
    {
//        $c_id = (isset($_GET['c_id']) && !empty($_GET['c_id'])) ? $_GET['c_id'] : 0;
        $res = db('market_trading_cid')->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '成功', $res);

        } else {
            $this->_jsonp('1000', '失败', null);
        }
    }

    public function index()
    {

        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
        $limit = (isset($_GET['limit']) && !empty($_GET['limit'])) ? $_GET['limit'] : 10;
        $username = (isset($_GET['username']) && !empty($_GET['username']) ? $_GET['username'] : null);
        $logmin = (isset($_GET['logmin']) && $_GET['logmin']) ? strtotime($_GET['logmin']) : 0;
        $logmax = (isset($_GET['logmax']) && $_GET['logmax']) ? (strtotime($_GET['logmax']) + 24 * 60 * 60 - 1) : time();

        $where['a.c_id'] = (isset($_GET['c_id']) && !empty($_GET['c_id'])) ? $_GET['c_id'] : array('neq', 0);
//        $where['a.category_id'] = (isset($_GET['category_id']) && !empty($_GET['category_id'])) ? $_GET['category_id'] : array('neq', 0);
//        $where['a.p_id'] = (isset($_GET['p_id']) && !empty($_GET['p_id'])) ? $_GET['p_id'] : 1;
        $where['a.static'] = (isset($_GET['static']) && !empty($_GET['static'])) ? $_GET['static'] : array('neq', 0);
        $where['a.time'] = array(array('egt', $logmin), array('elt', $logmax), 'and');
        $where['a.username'] = array('like', "%$username%");
        $where['a.status'] = 1;
        $where['a.is_show'] = 1;

        $join[0] = "__MARKET_TRADING_CATEGORY__ m";
        $join[1] = "m.id = a.category_id";

        $in[0] = "__SHOP_INFORMATION__ s";
        $in[1] = "s.id = a.name";

        $alias = 'a';
        $field = 'a.id,a.username,a.store,a.time,a.static,m.market_name,s.name';

        $res['data'] = db('market_trading')->alias($alias)->field($field)->join($join[0], $join[1])->join($in[0], $in[1])->limit($limit)
            ->where($where)->page($page)->select();
        $res['count'] = db('market_trading')->alias($alias)->where($where)->count();

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '交易市场管理查询成功', $res);
        } else {
            $this->_jsonp('1000', '交易市场管理查询失败', null);
        }
    }

    public function trading_msg()
    {
        $id = trim(input('get.id'));
        $where['f.id'] = $id;
        $join[0] = '__MARKET_TRADING_CATEGORY__ m';
        $join[1] = 'm.id = f.category_id';
        $in[0] = '__MARKET_TRADING_CID__ c';
        $in[1] = 'c.id = f.c_id';
        $in1[0] = "__SHOP_INFORMATION__ s";
        $in1[1] = "s.id = f.name";
        $alias = 'f';
        $field = 'f.*,c.trading_name,m.market_name,s.name as king';

        $res = db('market_trading')->alias($alias)->field($field)->join($join[0], $join[1])->join($in[0], $in[1])->join($in1[0], $in1[1])
            ->where($where)->find();

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }


    public function goods_pass()
    {
		$true = auth();
		if ($true == 2) {
			$id = trim(input('get.id'));
			$data['static'] = 2;
			$data['status'] = 1;
			$data['on_time'] = time();
			$where['id'] = explode(",", $id);
			for ($i = 0; $i < count($where['id']); $i++) {
				$res = db('market_trading')->where('id',$where['id'][$i])->update($data);
			}
			if (isset($res) || !empty($res)) {

				action_log('交易市场修改状态', 'log_admin', 1);
				$this->_jsonp('200', '操作成功', null);
			} else {
				action_log('交易市场修改状态', 'log_admin');
				$this->_jsonp('1000', '操作失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    public function goods_nopass()
    {
		$true = auth();
		if ($true == 2) {
			$where['id'] = trim(input('get.id'));
			$data['static'] = 3;
			$data['status'] = 2;
			$data['reson'] = trim(input('get.reson'));
			if (isset($where) || !empty($where)) {
				$zgh_member = new Data('market_trading');
				$zgh_member->_update($data, $where);
				action_log('交易市场修改状态', 'log_admin', 1);
				$this->_jsonp('200', '操作成功', null);
			} else {
				action_log('交易市场修改状态', 'log_admin');
				$this->_jsonp('1000', '操作失败', null);
			}
		} else if($true == 1) {
            $this->_jsonp('1000', '您没有权限', null);
        }else{
			$this->_jsonp('1000','您未登录',null);
		}
    }

    /**
     * 首页
     */
    public function trading_index()
    {
        $limit = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 20;
        $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

        $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
        $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

        if ($maxmoney) {
            $where['a.store'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
        } else {
            $where['a.store'] = array('egt', $minmoney);
        }

        $where['a.c_id'] = (isset($_GET['c_id']) && !empty($_GET['c_id'])) ? $_GET['c_id'] : array('neq', 0);
        $where['a.category_id'] = (isset($_GET['category_id']) && !empty($_GET['category_id'])) ? $_GET['category_id'] : array('neq', 0);
        $where['a.static'] = 2;
        $where['a.status'] = 1;
        $where['a.is_show'] = 1;
		$where['c.static'] = 1;
        $where['c.status'] = 2;

        $value = trim(input('value'));
        if ($value == 1 || $value == '1') {
            $order = 'value desc';
        } else if ($value == 3 || $value == '3') {
            $order = 'time desc';
        } else if ($value == 4 || $value == '4') {
            $order = 'store';
        } else if ($value == 5 || $value == '5') {
            $order = 'store desc';
        } else {
            $order = 'id';
        }

        $join[0] = "__SHOP_INFORMATION__ c";
        $join[1] = 'a.name = c.id';
        $field = "a.id,a.name as s_id,a.value,a.store,a.username,a.time,a.image,c.name";
        $alias = 'a';
        $res['data'] = db('market_trading')->where($where)->alias($alias)->field($field)->join($join[0], $join[1])->order($order)->limit($limit)->page($page)->select();
//        dump(db('market_trading')->getLastSql());
        $res['count'] = db('market_trading')->where($where)->alias($alias)->field($field)->join($join[0], $join[1])->count();

        //分页
        $Page = new \app\api\model\Page($res['count'], $limit);
        $render = $Page->render;

        $res['render'] = $render;

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '交易市场管理信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '交易市场管理信息查询失败', null);
        }
    }

    /**
     * 推荐交易信息
     */
    public function trading_rand()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 2;
        $order = trim(input('get.order'));
        $join[0] = "__SHOP_INFORMATION__ s";
        $join[1] = "s.id = a.name";
        $where['a.status'] = 1;
        $where['a.static'] = 2;
        $where['s.static'] = 1;
        $where['s.status'] = 2;
        $where['a.is_show'] = 1;
        $filed = "a.id,a.image,a.username,a.store";
        $res = db('market_trading')->alias('a')->field($filed)->join($join[0], $join[1])->where($where)->order($order)->limit($number)->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '推荐交易信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '推荐交易信息查询失败', null);
        }
    }

    /**
     * 交易类商品详情
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trading_details()
    {
        $where['a.id'] = trim(input('get.id'));
		$where['a.is_show'] = 1;
//        $where['a.static'] = 2;
//        $where['a.status'] = 1;
        $join[0] = "__MARKET_TRADING_CATEGORY__ m";
        $join[1] = "m.id = a.category_id";

        $in[0] = "__MARKET_TRADING_CID__ c";
        $in[1] = "c.id = a.c_id";

        $on[0] = "__SHOP_INFORMATION__ s";
        $on[1] = "s.id = a.name";

        $im[0] = "__SHOP_CATEGORY__ y";
        $im[1] = "y.id = s.shop_id";

        $alias = 'a';
        $field = 'a.id,a.static,a.status,a.order,a.volist,a.value,a.username as user,a.store,a.time,a.content,a.image,a.on_time,a.be_time,s.image as images,s.shop_id as s_id,
        m.id as category_id,m.market_name,c.trading_name,s.name,s.username,s.static as caking,s.shop_volume,s.label as shop_label,s.id as shop_id,y.shop_name,s.uid as user_id';

        $res = db('market_trading')->alias($alias)->field($field)
            ->join($join[0], $join[1])->join($in[0], $in[1])->join($on[0], $on[1])->join($im[0], $im[1])->where($where)->find();
		if(!$res['id']){
			$this->_jsonp('1000','查询失败',null);
		}else{
			$category = db('market_trading_category')->where('id', $res['category_id'])->value('pid');
			$res['category'] = db('market_trading_category')->where('id', $category)->value('market_name');

			$res['trading_count'] = db('market_trading')->where('name', $res['shop_id'])->where('status', 1)->count();
			$res['service_count'] = db('service_market')->where('s_id', $res['shop_id'])->where('status', 1)->where('state', 1)->count();

			$res['label'] = db('shop_label')->select();
             $res['patent_a'] = db('patent_a')->where('order', $res['order'])->find();
            $res['patent_b'] = db('patent_b')->where('order', $res['order'])->find();
            if ($res['patent_b']['RES']) {
                $res['patent_b']['res'] = json_decode($res['patent_b']['RES']);
            }
			if (isset($res) || !empty($res)) {
				$this->_jsonp('200', '交易信息查询成功', $res);
			} else {
				$this->_jsonp('1000', '交易信息查询失败', null);
			}
		}
    }

    /*
    *浏览历史记录
    *param  $num int 记录条数 默认10条 提示：cookie存数据大小有限制，一般为4K
    *param  $day int 记录保存时间 默认7天
    *param  $id string 商品id,分类 等等参数值 如cid,id参数值以处‘|’外的其他字符分割
    *return datastr string  信息id数据字符串
    */
    function value_views($num = 10, $day = 7, $id)
    {
        $id = isset($id) ? $id : 0;
        if (isset($_COOKIE['views'])) {
            //直接替换
            if ($num == 1) {
                setcookie("views", $id, time() + 3600 * $day);
                $datastr = $_COOKIE['views'];
            } else {
                $datastr = $_COOKIE['views'];
                $ids = explode(',', $datastr);
                //在限定记录数以内
                if (count($ids) < $num) {
                    //id是否已经存在
                    if (!in_array($id, $ids)) {
                        $datastr .= ',' . $id;
                        setcookie("views", $datastr, time() + 3600 * $day);
                    }
                } else {
                    if (!in_array($id, $ids)) {
                        $datastr = str_replace($ids[0] . ',', '', $datastr);
                        $datastr .= ',' . $id;
                        setcookie("views", $datastr, time() + 3600 * $day);
                    }
                }

            }
        } else {
            setcookie("views", $id, time() + 3600 * $day);
            //php的cookie不会及时生效 直接访问报错 需要刷新一次
            @$datastr = $_COOKIE['views'];
        }
        $this->_jsonp('200', '666', $datastr);
    }

    /**
     * 看了又看的信息查询
     */
    public function trading_shop()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 2;
        $id = trim(input('get.id'));
        $where['id'] = array('in', $id);
        $where['is_show'] = 1;
        $res = db('market_trading')->where($where)->order('rand()')->limit($number)->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '看了又看交易信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '看了又看信息查询失败', null);
        }
    }

    /**
     * 联系卖家
     * 商品信息
     */
    public function trading_detail()
    {
        $where['a.id'] = trim(input('get.id'));

        $field = 'a.store,a.id as uid,a.category_id,a.username,a.image';
        $alias = 'a';

        $res = db('market_trading')->alias($alias)->field($field)->where($where)->find();

        $category = db('market_trading_category')->where('id', $res['category_id'])->value('pid');
        $res['category'] = db('market_trading_category')->where('id', $category)->value('market_name');

        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '查询成功', $res);
        } else {
            $this->_jsonp('1000', '查询失败', null);
        }
    }

    /**
     * 联系卖家
     */
    public function goods_chat()
    {
        $data['goods_id'] = $_GET['id'];
        $data['from_uid'] = $_GET['from_uid'];
        $data['to_uid'] = $_GET['to_uid'];
        $data['content'] = $_GET['content'];
        $data['reply_type'] = 2;
        $data['time'] = time();
		$data['index'] = 1;

        $where1['to_uid'] = $data['to_uid'];
        $where1['from_uid'] = $data['from_uid'];
        $where1['reply_type'] = 2;
        $data1['index'] = 0;
        db('goods_chat')->where($where1)->update($data1);
        $res = db('goods_chat')->insert($data);
        if (isset($res) || !empty($res)) {
            action_log_index('商品联系卖家', 'log_ment', 1);
            $this->_jsonp('200', '新增成功', $res);
        } else {
            action_log_index('商品联系卖家', 'log_ment');
            $this->_jsonp('1000', '新增失败', null);
        }
    }

    /**
     * 收藏
     */
    public function goods_collect()
    {
        $where['mid'] = trim(input('get.id'));//收藏的id
        $where['uid'] = trim(input('get.uid'));
        $where['type'] = 1;
        $where['static'] = 0;
        $base = new News('collect');
        $like = $base->check_name($where);
        if (empty($like)) {
            // dump($data);die;
            //收藏用户写入
            $where['time'] = time();
            $map['id'] = input('get.id');
            $like_add = Db::name("collect")->insert($where);
            $add = Db::name("market_trading")->where($map)->setInc('collect');
            if ($like_add !== false) {
                action_log_index('收藏', 'log_ment', 1);
                $this->_jsonp('200', '收藏成功', null);
            } else {
                action_log_index('收藏', 'log_ment');
                $this->_jsonp('1000', '收藏失败', null);
            }
        } else {
            $this->_jsonp('1001', '已收藏', null);
        }
    }

    /**
     * 店铺收藏
     */
    public function info_collect()
    {
        $mid = trim(input('get.id'));//收藏的id
        $uid = trim(input('get.uid'));
        $where['mid'] = $mid;
        $where['uid'] = $uid;
        $where['type'] = array('in', array(5, 6));
        $shop_info = db('collect')->where($where)->count();
        if ($shop_info == 0 || $shop_info == null) {
            $data['mid'] = $mid;
            $data['uid'] = $uid;
            $data['time'] = time();
            $shop = db('shop_information')->where('id', $where['mid'])->find();
            if ($shop['shop_id'] == 1) {
                $data['type'] = 5;
            } else {
                $data['type'] = 6;
            }
            $res = db('collect')->insert($data);
            db('shop_information')->where('id', $where['mid'])->setInc('collect');
            if ($res == true) {
                $this->_jsonp('200', '收藏成功', null);
            } else {
                $this->_jsonp('1000', '收藏失败', null);
            }
        } else {
            $this->_jsonp('1001', '您已收藏', null);
        }
    }

    /**
     * 热门收藏
     */
    public function rand_goods()
    {
        $number = (isset($_GET['number']) && !empty($_GET['number'])) ? $_GET['number'] : 4;
        $field = 'id,username,image';
        $res = db('market_trading')->where("static", 2)->order('rand()')->field($field)->limit($number)->select();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '热门收藏信息查询成功', $res);
        } else {
            $this->_jsonp('1000', '热门收藏信息查询失败', null);
        }
    }

    /**
     * 报价页面
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function quote()
    {
        $where['id'] = trim(input('get.id'));
        $field = 'store';
        $res = db('market_trading')->where($where)->field($field)->find();
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '报价页面查询成功', $res);
        } else {
            $this->_jsonp('1000', '报价页面查询失败', null);
        }
    }

    /**
     * 报价
     */
    public function trading_quote()
    {
        $data['mid'] = trim(input('get.mid'));
        $data['uid'] = trim(input('get.uid'));
        $data['store'] = trim(input('get.store'));
        $data['time'] = time();
        if (floor($data['store']) == $data['store']) {
			$where1['status'] = 1;
			$where2['status'] = array('in',array(2,3,4,5,6,7,8));
			$name = db('orders')->where('name', $data['mid'])->where($where1)->find();
			$count = db('orders')->where('name',$data['mid'])->where($where2)->count();
            $where['id'] = $data['mid'];
            $id = db('market_trading')->where($where)->find();
            if ($id['value'] + $count >= $id['volist']) {
                $this->_jsonp('1000', '这个商品已达到最高授权', null);
                return false;
            } else {
                if (count($name) == 0) {
                    $requery['name'] = $data['mid'];
                    $requery['seller'] = db('shop_information')->where('id', $id['name'])->value('uid');
                    $form = new Form();
                    $requery['order'] = $form->rand_order();
                    $requery['time'] = time();
                    $requery['s_time'] = strtotime("+3 days");
                    $requery['services'] = 1;

                    $user = db('user')->where('uid', $requery['seller'])->find();
                    $requery['alipay'] = $user['alipay'];
                    $offer_id = db('offer')->insertGetId($data);
                    $data1['order_id'] = db('orders')->insertGetId($requery);
                    db('offer')->where('id', $offer_id)->update($data1);
                    action_log_index('商品报价', 'log_ment', 1);
                    $this->_jsonp('200', '报价成功', null);
                } else {
                    $data['order_id'] = $name['id'];
                    db('offer')->insert($data);
					action_log_index('商品报价', 'log_ment', 1);
                    $this->_jsonp('200', '报价成功', null);
                }
            }
        } else {
            $this->_jsonp('1000', '报价只能为整数', null);
        }
    }

    /**
     * 验证是否重复报价
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function trading_type()
    {
        $where['uid'] = trim(input('get.uid'));
        $where['mid'] = trim(input('get.mid'));
		$where['static'] = array('in', array(1, 2));
        $id = db('market_trading')->where('id', $where['mid'])->value('name');
        $uid = db('shop_information')->where('id', $id)->value('uid');
        if ($uid == $where['uid']) {
            $this->_jsonp('1000', '您不能购买自己的产品', null);
        } else {
            $res = db('offer')->where($where)->select();
            if ($res != true) {
                $this->_jsonp('200', '查询成功', $res);
            } else {
                $this->_jsonp('1000', '抱歉,您已报价', null);
            }
        }
    }

    /**
     * 进店逛逛
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function center()
    {
        $index = trim(input('get.index'));
        $where['a.id'] = trim(input('get.uid'));
        $where['a.static'] = 1;
        $where['a.status'] = 2;
        $join[0] = "__SHOP_CATEGORY__ c";
        $join[1] = "c.id = a.shop_id";

        $field = "a.id,a.uid as user_id,a.image,a.label,a.username,a.name,a.shop_volume,c.shop_name";
        $res = db('shop_information')->alias('a')->field($field)->join($join[0], $join[1])->where($where)->find();
        $res['label_shop'] = db('shop_label')->select();
		
		$where5['id'] = trim(input('get.uid'));
        $where5['static'] = 1;
        $where5['status'] = 2;
		db('shop_information')->where($where5)->setInc('shop_view');

        $where1['uid'] = input('get.user');
        $where1['mid'] = input('get.uid');
        $where1['static'] = 0;
        $where1['type'] = array('in', array(5, 6));
        $collect = db('collect')->where($where1)->count();
        if ($collect == 0) {
            $res['collect'] = 0;
        } else {
            $res['collect'] = 1;
        }
        if (!empty($res['id'])) {

            $where2['name'] = $res['id'];
            $where2['status'] = 1;
            $where2['static'] = 2;
            $where2['is_show'] = 1;
            $res['trading_count'] = db('market_trading')->where($where2)->count();

            $where3['s_id'] = $res['id'];
            $where3['state'] = 1;
            $where3['status'] = 1;
            $res['service_count'] = db('service_market')->where($where3)->count();


            if ($index == 1) {
                $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;

                $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
                $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

                if ($maxmoney) {
                    $where4['a.store'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
                } else {
                    $where4['a.store'] = array('egt', $minmoney);
                }

                $where4['a.static'] = 2;
                $where4['a.status'] = 1;
                $where4['a.is_show'] = 1;
                $where4['c.static'] = 1;
                $where4['c.status'] = 2;
                $where4['a.name'] = $res['id'];

                $value = trim(input('value'));

                if ($value == 1 || $value == '1') {
                    $order = 'value desc';
                } else if ($value == 2 || $value == '2') {
                    $order = 'time desc';
                }else if ($value == 4 || $value == '4') {
                    $order = 'store';
                } else if ($value == 5 || $value == '5') {
                    $order = 'store desc';
                } else {
                    $order = 'id';
                }

                $join1[0] = "__SHOP_INFORMATION__ c";
                $join1[1] = 'a.name = c.id';
                $field = "a.id,a.value,a.store,a.username,a.time,a.image,c.name";
                $alias = 'a';
                $res['trading'] = db('market_trading')->where($where4)->alias($alias)->field($field)->join($join1[0], $join1[1])->order($order)->limit(20)->page($page)->select();
//        dump(db('market_trading')->getLastSql());
                $res['count'] = db('market_trading')->where($where4)->alias($alias)->field($field)->join($join1[0], $join1[1])->count();


                //分页
                $Page = new \app\api\model\Page($res['count'], 20);
                $render = $Page->render;

                $res['render'] = $render;
            } else {

                $page = (isset($_GET['page']) && !empty($_GET['page'])) ? $_GET['page'] : 1;
                $minmoney = (isset($_GET['minmoney']) && $_GET['minmoney']) ? $_GET['minmoney'] : 0;
                $maxmoney = (isset($_GET['maxmoney']) && $_GET['maxmoney']) ? $_GET['maxmoney'] : 0;

                if ($maxmoney) {
                    $where6['w.price'] = array(array('egt', $minmoney), array('elt', $maxmoney), 'and');
                } else {
                    $where6['w.price'] = array('egt', $minmoney);
                }

                $value = trim(input('value'));
                if ($value == 1 || $value == '1') {
                    $order = 'value desc';
                } else if ($value == 2 || $value == '2') {
                    $order = 'rate desc';
                } else if ($value == 3 || $value == '3') {
                    $order = 'time desc';
                } else if ($value == 4 || $value == '4') {
                    $order = 'store';
                } else if ($value == 5 || $value == '5') {
                    $order = 'store desc';
                } else {
                    $order = 'id';
                }
                $where6['w.state'] = 1;
                $where6['w.status'] = 1;
				
                $where6['i.static'] = 1;
                $where6['i.status'] = 2;
                $where6['w.s_id'] = $res['id'];

                $join2[0] = '__SHOP_INFORMATION__ i';
                $join2[1] = 'w.s_id=i.id';
                $fields = 'w.id,w.volume as value,w.rate ,w.price as store,w.goods_name as username,w.add_time as time,w.pic as image,i.name';
                $res['service'] = db('service_market')->order($order)->limit(20)->page($page)
                    ->alias('w')->field($fields)->join($join2[0], $join2[1])->where($where6)->select();
                $res['count'] = db('service_market')->alias('w')->field($fields)->join($join2[0], $join2[1])->where($where6)->count();
                //分页
                $Page = new \app\api\model\Page($res['count'], 20);
                $render = $Page->render;

                $res['render'] = $render;
            }
        } else {
            $res = null;
            $this->_jsonp('200', '查询失败', null);
        }
        if (isset($res) || !empty($res)) {
            $this->_jsonp('200', '我的店铺查询成功', $res);
        } else {
            $this->_jsonp('1000', '我的店铺查询失败', null);
        }
    }

    /**
     * 发送消息
     */
    public function trading_chat()
    {
        $data['to_uid'] = $_GET['to_uid'];
        $data['from_uid'] = $_GET['from_uid'];
        $data['content'] = $_GET['content'];
        $data['reply_type'] = 2;
        $data['time'] = time();
		$data['index'] = 1;

        $where1['to_uid'] = $data['to_uid'];
        $where1['from_uid'] = $data['from_uid'];
        $where1['reply_type'] = 2;
        $data1['index'] = 0;
        db('goods_chat')->where($where1)->update($data1);
        $res = db('goods_chat')->insert($data);
        if ($res) {
            action_log_index('联系卖家', 'log_ment', 1);
            $this->_jsonp('200', '新增成功', $res);
        } else {
            action_log_index('联系卖家', 'log_ment');
            $this->_jsonp('1000', '新增失败', null);
        }
    }

}